package com.zmovie.admin.controller.user;

import cn.hutool.core.util.ArrayUtil;
import com.zmovie.admin.constant.Constants;
import com.zmovie.common.error.BusinessException;
import com.zmovie.common.error.EnumBusinessError;
import com.zmovie.common.response.CommonReturnType;
import com.zmovie.framework.domain.UserRole;
import com.zmovie.framework.service.UserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * @author 挚爱之夕
 * @version 1.0
 * @implSpec com.zmovie.admin.controller.user
 * @since 2023-12-05 10:23
 */
@Controller
@RequestMapping("/user/role")
@Api(tags = "用户角色管理")
public class UserRoleController {
    @Autowired
    private UserRoleService userRoleService;
    @GetMapping
    @ApiOperation("用户角色管理页面")
    public String index(Model model){
        List<UserRole> userRoles = userRoleService.listUserRole(new UserRole());
        model.addAttribute(Constants.USER_ROLES_KEY, userRoles);
        return "user/user_role";
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id获取用户角色")
    @ResponseBody
    public Object getUserRole(@PathVariable("id") @NotNull Integer id){
        return CommonReturnType.success(userRoleService.getById(id));
    }

    @PostMapping
    @ApiOperation("添加用户角色")
    @ResponseBody
    public Object addUserRole(@Validated UserRole userRole){
        userRoleService.save(userRole);
        return CommonReturnType.success("添加成功!");
    }

    @PutMapping
    @ResponseBody
    @ApiOperation("修改用户角色")
    public Object updateRole(@Validated UserRole userRole) throws BusinessException {
        if(userRole.getId() != null){
            throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        userRoleService.updateById(userRole);
        return CommonReturnType.success("修改成功!");
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除用户角色")
    @ResponseBody
    public Object deleteUserRole(@PathVariable("id") @NotNull Integer id){
        userRoleService.removeById(id);
        return CommonReturnType.success("删除成功!");
    }

    @DeleteMapping
    @ApiOperation("批量删除用户角色")
    @ResponseBody
    public Object deleteUserRoleBatch(Integer[] id) throws BusinessException {
        if(ArrayUtil.isEmpty(id)){
            throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        userRoleService.removeByIds(Arrays.asList(id));
        return CommonReturnType.success("批量删除成功!");
    }

    @PostMapping("/search")
    @ApiOperation("搜索")
    public String search(UserRole userRole, Model model){
        List<UserRole> userRoles = userRoleService.listUserRole(userRole);
        model.addAttribute(Constants.USER_ROLES_KEY, userRoles);
        return "user/user_role :: userRoleList";
    }
}
